#include "org_cocos2dx_cpp_JavaCallCppHelper.h"
#include "cocos2d.h"
#include <string.h>
#include <android/log.h>

#include <jni/JniHelper.h>
#include <jni.h>

using namespace cocos2d;

#define LOG_TAG "JNI"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

char* jstringTostring(JNIEnv* env, jstring jstr)
{
	char* rtn = NULL;
	jclass clsstring = env->FindClass("java/lang/String");
	jstring strencode = env->NewStringUTF("utf-8");
	jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
	jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
	jsize alen = env->GetArrayLength(barr);
	jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
	if (alen > 0)
	{
		rtn = (char*)malloc(alen + 1);
		memcpy(rtn, ba, alen);
		rtn[alen] = 0;
	}
	env->ReleaseByteArrayElements(barr, ba, 0);
	return rtn;
}

JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_JavaCallCppHelper_postNotification
  (JNIEnv * env, jclass obj, jstring jKey, jstring jJsonData){

	const char* pszKey = jstringTostring(env, jKey);
	const char* pszJsonData = jstringTostring(env, jJsonData);
	LOGI("JNICALL_postNotification: key = %s, data = %s", pszKey, pszJsonData);
	//发送通知
	CCNotificationCenter::sharedNotificationCenter()->postNotification(pszKey, CCString::create(pszJsonData));
	//释放内存
	env->ReleaseStringUTFChars(jKey, pszKey);
	env->ReleaseStringUTFChars(jJsonData, pszJsonData);
}

void doAuthorize(int platform) {

}


